package com.Sumcc.setting.Interceptor;


import com.Sumcc.common.constant.Constant;
import com.Sumcc.setting.Interceptor.ApiInterceptor;
import com.Sumcc.utils.systemUtil.PropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

/**
 *
 * @author Sumcc
 */
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {


    private final ApiInterceptor apiInterceptor;
    @Autowired
    public WebMvcConfig(ApiInterceptor apiInterceptor) {
        this.apiInterceptor = apiInterceptor;
    }

    /**
     * 拦截器配置
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 多个拦截器组成一个拦截器链  可以写多个 registry.
        registry.addInterceptor(apiInterceptor)
                .addPathPatterns("/" + Constant.API_PATH + "/**")
                .excludePathPatterns(
                        "/"+Constant.API_PATH+"/user/login"
                ,"/src/img/**");

    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/src/img/**").addResourceLocations("file:" + gteProperties() + "/upload/wyxt/");
    }
    /**
     * 获取盘符地址
     *
     * @return
     */
    public String gteProperties() {
        return PropertiesUtil.getStorageAddress();
    }
}
